<?php
require_once("../../bbs/model/conn.php");
/**
 * 获取所有帖子信息
 * @return array
 */
function get_all_posts()
{
    $sql = "SELECT b.*,t.*,u.*,( SELECT COUNT(*) FROM tbl_reply r WHERE r.topicId = t.topicId ) AS replyNumber FROM tbl_board b,tbl_topic t,tbl_user u WHERE b.boardid = t.boardId AND t.uId = u.uId order by t.topicId DESC;";
    // 调用conn.php 的exec_select()函数，返回二维数组
    return exec_select($sql);
}
/**
 * 获取所有版块信息
 * @return array
 */
function get_all_boards()
{
    $sql = "select * from tbl_board";
    // 调用conn.php 的exec_select()函数，返回二维数组
    return exec_select($sql);
}
/**
 * 删除帖子
 * @param int $topic_id 帖子ID
 * @return bool
 */
function delete_post($topic_id)
{
    $sql = "delete from tbl_topic where topicid = $topic_id";
    return exec_update($sql);
}
/**
 * 获取指定版块的帖子列表
 * @param int $boardId 版块ID
 * @return array
 */
function get_posts_by_board($boardId)
{
    $sql = "SELECT topicId FROM tbl_topic WHERE boardId = $boardId";
    return exec_select($sql);
}
/**
 * 设置帖子为精华帖
 * @param int $topicId 帖子ID
 * @return bool
 */
function set_post_good($topicId, $isgood)
{
    $sql = "update tbl_topic set good = $isgood where topicid = $topicId";
    return exec_update($sql);
}
/**
 * 添加版块
 * @param string $boardName 版块名称
 * @return bool
 */
function add_board($boardName)
{
    // 先查询是否已存在
    $sql_check = "SELECT COUNT(*) as cnt FROM tbl_board WHERE boardname = '$boardName'";
    $result = exec_select($sql_check);
    if ($result && $result[0]['cnt'] > 0) {
        // 已存在，返回false或自定义提示
        return false;
    }
    // 不存在再插入
    $sql = "insert into tbl_board(boardname) values('$boardName')";
    return exec_update($sql);
}
/**
 * 删除版块
 * @param int $boardId 版块ID
 * @return bool
 */
function delete_board($boardId)
{
    $sql = "delete from tbl_board where boardid = $boardId";
    return exec_update($sql);
}
?>